//
//  NSString-NumericCompare.m
//  iContractor
//
//  Created by Jeff LaMarche on 1/26/09.
//  Copyright 2009 Jeff LaMarche Consulting. All rights reserved.
//

#import "NSString-NumericCompare.h"


@implementation NSString(NumericCompare)
- (NSComparisonResult)numericCompare:(NSString *)compare
{
	// enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
	int myValue = [self intValue];
	int compareValue = [compare intValue];
	
	if (myValue < compareValue)
		return NSOrderedAscending;
	else if (myValue > compareValue)
		return NSOrderedDescending;
	else
		return NSOrderedSame;
}
- (NSComparisonResult)reverseNumericCompare:(NSString *)compare
{
	return [self numericCompare:compare] * -1;
}
- (NSComparisonResult)currencyCompare:(NSString *)compare
{
	NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
	NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
	
	NSString *meStripped = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	meStripped = [meStripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:groupingSeparator]];
	meStripped = [meStripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:currencySymbol]];
	
	NSString *compareStripped = [compare stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	compareStripped = [compareStripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:groupingSeparator]];
	compareStripped = [compareStripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:currencySymbol]];
	
	return [meStripped numericCompare:compareStripped];
}
- (NSComparisonResult)reverseCurrencyCompare:(NSString *)compare
{
	return [self currencyCompare:compare] * -1;
}
@end
